package tests;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.GregorianCalendar;

import main.*;

import org.junit.Before;
import org.junit.Test;

/**
 * ConferenceTest Class
 * @author Holly Beach
 * updated constructor tests to include additional fields Holly 
 * added tests for addUser
 * Cristopher removed addRole(author) because of added business rule
 * 
 * @version 12/3
 */
public class ConferenceTest {
	
	Manuscript manuscript_1;
	Manuscript manuscript_2;
	ArrayList<Manuscript> manu_list;
	GregorianCalendar manu_deadline;
	GregorianCalendar rev_deadline;
	ArrayList<User> user_list;
	Conference conf_1;
	Conference conf_2;
	
	@Before
	public void setUp() throws Exception {
		manuscript_1 = new Manuscript();
		User anAuth = new User("user@gmail.com", "Tom Smarty");
		manuscript_2 = new Manuscript("Paper1", anAuth.getID(), 
										anAuth.getName(), "Text");
		manu_list = new ArrayList<Manuscript>(); 
		manu_list.add(manuscript_1);
		manu_deadline = new GregorianCalendar(2011, 12, 31);
		rev_deadline = new GregorianCalendar(2012, 3, 31);
		user_list = new ArrayList<User>();
		user_list.add(anAuth);
		conf_1 = new Conference();
		conf_2 = new Conference("Conference A", manu_deadline, rev_deadline, 
								manu_list, user_list);

	}

	/**
	 * @author Holly 11/18/11
	 * Test for the Default Constructor
	 */
	@Test
	public void testDefaultConstructor() {
		Conference c = new Conference();
		assertEquals("Name initially 'Default_Name'", "Default_Name", c.getConfName());
		assertEquals("Manuscript submission deadline initially 12/5/2011?", 
						new GregorianCalendar(2011,12,5), c.getManuscriptDeadline()); 
		assertEquals("Review submission deadline initially 3/5/2012?", 
						new GregorianCalendar(2012,3,5), c.getReviewDeadline());
		assertEquals("Manuscript List initially size 0?", 0, c.getManuscripts().size());
		assertEquals("User List initially size 0?", 0, c.getUsers().size());
	}
	
	/**
	 * @author Holly 11/18/11
	 */
	@Test
	public void testConstructorWithParams() {
		assertEquals("Name initially 'Conference A'", "Conference A", conf_2.getConfName());
		assertEquals("Manuscript submission deadline correct?", 
						new GregorianCalendar(2011,12,31), conf_2.getManuscriptDeadline());
		assertEquals("Manuscript submission deadline initially 12/5/2011?", 
						new GregorianCalendar(2012,3,31), conf_2.getReviewDeadline());
		assertEquals("Manuscript List initially size 1?", 1, conf_2.getManuscripts().size());
		assertEquals("manuscript_1 in the my_manuscripts?",manuscript_1, 
						conf_2.getManuscripts().get(0));
		assertEquals("User List initially size 1?", 1, conf_2.getUsers().size());
	}
	
	
	
	/**
	 * @author Holly 11/18/11 
	 * Tests the setStatus method
	 */
	@Test
	public void testSetStatus(){
		conf_2.setStatus(manuscript_1, Status.APPROVED);
		
		assertEquals("status is APPROVED?", Status.APPROVED, 
				manuscript_1.getStatus());
		
		conf_2.setStatus(manuscript_1, Status.REJECTED);
		assertEquals("status is REJECTED?", Status.REJECTED, 
				manuscript_1.getStatus());
		
		conf_2.setStatus(manuscript_1, Status.UNDECIDED);
		assertEquals("status is UNDECIDED?", Status.UNDECIDED, 
				manuscript_1.getStatus());		
	}
	
	/**
	 * @author Holly 11/22/11
	 * Tests to make sure a Manuscript object created from pulling a 
	 * Manuscript out of the list of Manuscripts will update correctly in
	 * the list of manuscripts.
	 */
	@Test
	public void testSetStatusWithCopiedManuscriptObject(){
		conf_2.addManuscript(manuscript_2);
		Manuscript differentManuscriptObj = conf_2.getManuscripts().get(1);
		
		conf_2.setStatus(differentManuscriptObj, Status.REJECTED);
		
		assertEquals("Did the status of manuscript within list of manuscripts update?",
				Status.REJECTED, conf_2.getManuscripts().get(1).getStatus());
		
		ArrayList<Manuscript> testList = conf_2.getManuscripts();
		Manuscript testManu = testList.get(0);
		conf_2.setStatus(testManu, Status.APPROVED);
		assertEquals("Did the status of manuscript within list of conf manuscripts update?",
				Status.APPROVED, conf_2.getManuscripts().get(0).getStatus());
		
	}
	
	/**
	 * @author Holly 11/21/11
	 */
	@Test
	public void testGetUsers() {//make sure it returns a copy
		assertFalse("Is the returned list a copy?", user_list == conf_2.getUsers());
		assertEquals(user_list, conf_2.getUsers());
	}
	
	/**
	 * @author Holly 11/21/11
	 */
	@Test
	public void testAddUser() {
		int size = conf_2.getUsers().size();
		User user = new User("xyz@blah.com", "Mary Jones");
		conf_2.addUser(user);
		assertTrue("Does list contain newly added user?", conf_2.getUsers().contains(user));
		assertEquals("Did List size increase?", size+1, conf_2.getUsers().size());
	}
	
	/**
	 * @author Holly 11/21/11
	 */
	@Test
	public void testAddManuscript() {
		int manuListSize = conf_2.getManuscripts().size();
		conf_2.addManuscript(manuscript_2);
		
		assertTrue("Does List contain new manuscript?", 
				conf_2.getManuscripts().contains(manuscript_2));
		assertEquals("Was size of manuscript list increased?", manuListSize+1, 
						conf_2.getManuscripts().size());
		
		//test another manuscript and an updated version
		Manuscript manu_3 = new Manuscript("Paper3", "new ID",
				"new_auth", "Textwithoutupdate");
		conf_2.addManuscript(manu_3);
		assertTrue("Does List contain new manuscript?", 
				conf_2.getManuscripts().contains(manu_3));
		assertEquals("Was size of manuscript list increased?", manuListSize+2, 
						conf_2.getManuscripts().size());
		//update and test
		Manuscript update = new Manuscript("Paper3", "new ID",
				"new_auth", "Updated text");
		assertTrue("does .equals data old/updated match?", 
				conf_2.getManuscripts().contains(update));
		assertEquals("Updated size unchanged?", manuListSize+2, 
				conf_2.getManuscripts().size());
		assertFalse("was text updated?", conf_2.getManuscripts().get(manuListSize+1)
				.getText().equals("Updated text"));
	}
		
	/**
	 * @author Holly 11/21/11
	 */
	@Test
	public void testDeleteManuscript(){
		int manuListSize = conf_2.getManuscripts().size();
		conf_2.deleteManuscript(manuscript_1);
		
		assertFalse("Did manuscript_1 get removed?", 
					conf_2.getManuscripts().contains(manuscript_1));
		assertEquals("Was size of manuscript list decresed?", manuListSize-1, 
					conf_2.getManuscripts().size());
	}
	
	//TODO remove this?
	/**
	 * @author Holly 11/21/11
	 * 
	@Test
	public void testUpdateManuscript(){
		conf_2.addManuscript(manuscript_2);
		int manuListSize = conf_2.getManuscripts().size();
		Manuscript updatedManuscript2 = new Manuscript("Paper1", manuscript_2.getAuthorID(), 
										manuscript_2.getAuthor(), "Paper1Revised");
		

		conf_2.updateManuscript(manuscript_2, updatedManuscript2);
		
		assertFalse("Did manuscript_1 get removed?", 
					conf_2.getManuscripts().contains(manuscript_2));
		assertTrue("Did updatedManuscript2 get added?",
				conf_2.getManuscripts().contains(updatedManuscript2));
		assertEquals("Is size of manuscript list unchanged?", manuListSize, 
					conf_2.getManuscripts().size());
	}*/
	
	/**
	 * @author Holly 11/22/11
	 */
	@Test
	public void testGetManuscriptsForAuthor() {
		User user1 = new User("zzz@abc.com", "Henry Jones");
		user1.addRole(Roles.REVIEWER);

		Manuscript userAuthored1 = new Manuscript("Something", user1.getName(), 
				user1.getID(), "SomethingText");
		conf_1.addManuscript(userAuthored1);
		conf_1.addUser(user1);
		
		ArrayList<Manuscript> user1Manus = conf_1.getManuscriptsForAuthor(user1);
		
		assertEquals("Is size 1?", 1, user1Manus.size());
		assertTrue("Is the correct manuscript in there?", user1Manus.contains(userAuthored1));		
	}
	
	/**
	 * @author Holly 11/22/11
	 */
	@Test
	public void testGetManuscriptsForReviewer() {
		User user1 = new User("zzz@abc.com", "Henry Jones");
		user1.addRole(Roles.REVIEWER);
		manuscript_1.addReviewer(user1.getID());
		conf_2.addUser(user1);
		conf_2.addManuscript(manuscript_2);;
		
		Manuscript anotherManuForUser1 = new Manuscript();
		anotherManuForUser1.addReviewer(user1.getID());
		conf_2.addManuscript(anotherManuForUser1);
		
		ArrayList<Manuscript> usersManusToReview = conf_2.getManuscriptsForReviewer(user1);
		
		assertTrue("is manuscript_1 in list?", usersManusToReview.contains(manuscript_1));
		assertFalse("is manuscript_2 in list?", usersManusToReview.contains(manuscript_2));
		assertTrue("is anotherManuForUser1 in list?", 
				usersManusToReview.contains(anotherManuForUser1));
	}
	
}
